package com.wsm.kotlintest.model.datafactory

import androidx.lifecycle.MutableLiveData
import androidx.paging.DataSource
import com.wsm.kotlintest.common.*
import com.wsm.kotlintest.model.datamodel.BaseModel
import com.wsm.kotlintest.model.response.redditResponseModel.Children
import com.wsm.kotlintest.model.response.redditResponseModel.Data
import com.wsm.kotlintest.model.datamodel.TestNetDataModel
import com.wsm.kotlintest.viewmodel.ui.DataResponseCallBack
import com.wsm.kotlintest.viewmodel.ui.NetStateCallBack

/**
 * Created by 王家宝 on 2018/7/18.
 * TEL:18241150668
 * MAIL:639586819@qq.com
 */
class DataMaker(
        private val netState: MutableLiveData<Boolean>,
        private val refreshing: MutableLiveData<Boolean>,
        private val loadMoreState: MutableLiveData<Int>) : DataSource.Factory<String, Children>() {

    override fun create(): DataSource<String, Children> {
        return MyData(netState, refreshing, loadMoreState)
    }

    inner class MyData(netState: MutableLiveData<Boolean>,
                       refreshing: MutableLiveData<Boolean>,
                       loadMoreState: MutableLiveData<Int>) : BaseDataSource<String, Children>(netState, refreshing, loadMoreState) {

        override var dataModel: BaseModel
            get() = TestNetDataModel()
            set(value) {}


        override fun init(params: LoadInitialParams<String>, callback: LoadInitialCallback<String, Children>) {

            requestApi(go().getDataFromNet("dalian", limit = params.requestedLoadSize),
                    object : DataResponseCallBack<Data> {
                        override fun finalResponse(data: Data) {
                            callback.onResult(data.children ?: emptyList<Children>(),
                                    null,
                                    if (data.after != null) {
                                        loadMoreState.value = LOAD_MORE_STATE_SUCCESS
                                        data.after
                                    } else {
                                        loadMoreState.value = LOAD_MORE_STATE_NO_DATA
                                        null
                                    })
                        }

                    })
        }

        override fun onNext(params: LoadParams<String>, callback: LoadCallback<String, Children>) {
            requestApi(go().getDataFromNet("dalian", params.key, params.requestedLoadSize), object : DataResponseCallBack<Data> {
                override fun finalResponse(data: Data) {
                    callback.onResult(data.children ?: emptyList<Children>(),
                            if (data.after != null) {
                                loadMoreState.value = LOAD_MORE_STATE_SUCCESS
                                data.after
                            } else {
                                loadMoreState.value = LOAD_MORE_STATE_NO_DATA
                                null
                            })
                }

            },type = Type.NEXT)
        }

    }
}